<html>
<head>
  <title>tensorflow: 变量（Variable）</title>
  <basefont face="微软雅黑" size="2" />
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
  <meta name="exporter-version" content="Evernote Windows/307027 (zh-CN, DDL); Windows/6.1.0 (Win32);"/>
  <style>
    body, td {
      font-family: 微软雅黑;
      font-size: 10pt;
    }
  </style>
</head>
<body>
<a name="499"/>
<h1>tensorflow: 变量（Variable）</h1>

<div>
<span><div style="text-align: center;"><font style="font-size: 24pt;"><span style="font-size: 24pt; color: rgb(28, 51, 135); font-weight: bold;">变量</span></font></div><div style="text-align: center;"><div><br/></div></div><div style="text-align: center;"><br/></div><div style="text-align: left;"><span style="font-size: 18pt; color: rgb(28, 51, 135);">变量：创建、初始化、保存和加载</span></div><div style="text-align: left;"><div>        <span style="font-size: 12pt;">当训练模型时，用</span><a href="http://www.tensorfly.cn/tfdoc/api_docs/python/state_ops.html" style="font-size: 12pt; color: rgb(173, 0, 0);">变量</a><span style="font-size: 12pt;">来</span><font style="font-size: 14pt;"><span style="font-size: 14pt; color: rgb(173, 0, 0); font-weight: bold;">存储和更新参数</span></font><span style="font-size: 12pt;">。变量包含张量 (Tensor)存放于内存的缓存区。建模时它们需要被明确地初始化，模型训练后它们必须被存储到磁盘。这些变量的值可在之后模型训练和分析是被加载。</span></div></div><div style="text-align: left;"><br/></div><div style="text-align: left;"><div><br/></div></div><div style="text-align: left;"><ul><li><font color="#AD0000" style="font-size: 16pt;"><span style="font-size: 16pt; color: rgb(173, 0, 0); font-weight: bold;">tf.Variable 类</span></font></li></ul><div><div><br/></div><table style="border-collapse: collapse; min-width: 100%;"><colgroup><col style="width: 720px;"></col></colgroup><tbody><tr><td style="border: 1px solid rgb(204, 204, 204); width: 720px; padding: 8px;"><div><span style="font-size: 12pt; color: rgb(173, 0, 0); font-weight: bold; line-height: 1.45;">创建：</span></div><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px;"><div><span style="font-size: 12pt; color: rgb(173, 0, 0); font-weight: bold; line-height: 1.45;">当创建一个</span><a href="http://www.tensorfly.cn/tfdoc/api_docs/python/state_ops.html" style="font-size: 12pt; color: rgb(173, 0, 0); font-weight: bold; line-height: 1.45;">变量</a><span style="font-size: 12pt; color: rgb(173, 0, 0); font-weight: bold; line-height: 1.45;">时，你将一个张量作为初始值传入构造函数Variable()。TensorFlow提供了一系列操作符来初始化张量，初始值是</span><a href="http://www.tensorfly.cn/tfdoc/api_docs/python/constant_op.html" style="font-size: 12pt; color: rgb(173, 0, 0); font-weight: bold; line-height: 1.45;">常量或是随机值</a><span style="font-size: 12pt; color: rgb(173, 0, 0); font-weight: bold; line-height: 1.45;">。</span><span style="font-size: 12pt; color: rgb(173, 0, 0); font-weight: bold; line-height: 1.45;">注意，所有这些操作符都需要你指定张量的shape。那个形状自动成为变量的shape。变量的shape通常是固定的，但TensorFlow提供了高级的机制来重新调整其行列数。</span></div></blockquote></td></tr><tr><td style="border: 1px solid rgb(204, 204, 204); width: 720px; padding: 8px;"><div><span style="font-size: 12pt; color: rgb(173, 0, 0); font-weight: bold;">初始化：</span></div><ol><li><span style="font-size: 12pt;">使用tf.initialize_all_variables()添加一个操作对变量做初始化。记得在完全构建好模型并加载之后再运行那个操作。</span></li><li><span style="font-size: 12pt;">由另一个变量初始化：</span><span style="font-size: 12pt;">用另一个变量的初始化值给当前变量初始化。</span></li><li><span style="font-size: 12pt;">自定义初始化：</span><span style="font-size: 12pt;">tf.initialize_all_variables()函数便捷地添加一个op来初始化模型的所有变量。你也可以给它传入一组变量进行初始化。</span></li></ol></td></tr><tr><td style="border: 1px solid rgb(204, 204, 204); width: 720px; padding: 8px;"><div><span style="font-size: 12pt;">保存和加载：</span></div><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px;"><div><font style="font-size: 12pt;"><span style="font-size: 12pt; color: rgb(173, 0, 0); font-weight: bold;">最简单的保存和恢复模型的方法是使用tf.train.Saver对象。构造器给graph的所有变量，或是定义在列表里的变量，添加save和restoreops。saver对象提供了方法来运行这些ops，定义检查点文件的读写路径。</span></font></div></blockquote></td></tr><tr><td style="border: 1px solid rgb(204, 204, 204); width: 720px; padding: 8px;"><div>W = tf.Variable()</div></td></tr></tbody></table><div><br/></div></div><div><br/></div><ul><li><font color="#AD0000" style="font-size: 16pt;"><span style="font-size: 16pt; color: rgb(173, 0, 0); font-weight: bold;">tf.train.Saver 类</span></font></li></ul></div><div style="text-align: left;"><br/></div><div style="text-align: left;"><div>用tf.train.Saver()创建一个Saver来管理模型中的<font style="font-size: 14pt;"><span style="font-size: 14pt; color: rgb(173, 0, 0); font-weight: bold;">所有</span></font>变量。</div></div><div style="text-align: left;"><br/></div><div style="text-align: left;"><div><br/></div><table style="border-collapse: collapse; min-width: 100%;"><colgroup><col style="width: 593px;"></col></colgroup><tbody><tr><td style="border: 1px solid rgb(204, 204, 204); width: 593px; padding: 8px;"><div><font style="font-size: 14pt;"><span style="font-size: 14pt; color: rgb(173, 0, 0); font-weight: bold;">保存变量：</span></font></div><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px;"><div>用tf.train.Saver()创建一个Saver来管理模型中的<span style="font-size: 14pt; color: rgb(173, 0, 0); font-weight: bold;">所有</span>变量。</div></blockquote></td></tr><tr><td style="border: 1px solid rgb(204, 204, 204); width: 593px; padding: 8px;"><div><font style="font-size: 14pt;"><span style="font-size: 14pt; color: rgb(173, 0, 0); font-weight: bold;">恢复变量：</span></font></div><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px;"><div>用同一个Saver对象来恢复变量。注意，当你从文件中恢复变量时，不需要事先对它们做初始化。</div></blockquote></td></tr><tr><td style="border: 1px solid rgb(204, 204, 204); width: 593px; padding: 8px;"><h3><span style="color: rgb(173, 0, 0);">选择存储和恢复哪些变量：</span></h3><div><ol><li><span style="color: rgb(173, 0, 0);">不给tf.train.Saver()传入任何参数，那么saver将处理graph中的所有变量。其中每一个变量都以变量创建时传入的名称被保存。、</span></li><li><span style="color: rgb(173, 0, 0);">有时候在检查点文件中明确定义变量的名称很有用。举个例子，你也许已经训练得到了一个模型，其中有个变量命名为&quot;weights&quot;，你想把它的值恢复到一个新的变量&quot;params&quot;中。</span></li><li><span style="color: rgb(173, 0, 0);">保存和恢复模型的一部分变量很有用，<span style="color: rgb(173, 0, 0);">你可以通过给tf.train.Saver()构造函数传入Python字典，很容易地定义需要保持的变量及对应名称：键对应使用的名称，值对应被管理的变量。</span></span></li></ol></div></td></tr></tbody></table><div><br/></div></div><div style="text-align: left;">保存变量过程：</div><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;"># Create some variables.</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">v1 = tf.Variable(..., name=&quot;v1&quot;)</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">v2 = tf.Variable(..., name=&quot;v2&quot;)</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">...</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;"># Add an op to initialize the variables.</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">init_op = tf.initialize_all_variables()</span></div><div><br style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"/></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;"># Add ops to save and restore all the variables.</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">saver = tf.train.Saver()</span></div><div><br style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"/></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;"># Later, launch the model, initialize the variables, do some work, save the# variables to disk.</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">with tf.Session() as sess:</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">    sess.run(init_op)</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">    # Do some work with the model.</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">    ..</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">    # Save the variables to disk.</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">    save_path = saver.save(sess, &quot;/tmp/model.ckpt&quot;)</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">    print &quot;Model saved in file: &quot;, save_path</span></div></div><div style="text-align: left;"><br/></div><div style="text-align: left;">恢复变量过程：</div><div style="text-align: left;"><br/></div><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;"># Create some variables.</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">v1 = tf.Variable(..., name=&quot;v1&quot;)</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">v2 = tf.Variable(..., name=&quot;v2&quot;)</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">...</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;"># Add ops to save and restore all the variables.</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">saver = tf.train.Saver()</span></div><div><br style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"/></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;"># Later, launch the model, use the saver to restore variables from disk, and# do some work with the model.with tf.Session() as sess:</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">    # Restore variables from disk.</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">    saver.restore(sess, &quot;/tmp/model.ckpt&quot;)</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">    print &quot;Model restored.&quot;</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">    # Do some work with the model</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">    ...</span></div></div><div style="text-align: left;"><br/></div><div style="text-align: left;"><br/></div><div style="text-align: left;"><br/></div><div style="text-align: left;"><ul><li><b style="color: rgb(173, 0, 0); font-size: 16pt; line-height: 1.45;">tf.app.flags</b><br/></li></ul></div><div style="text-align: left;"><br/></div><div style="text-align: left;">tf定义了tf.app.flags，用于支持接受命令行传递参数，相当于接受argv。<br/></div><div style="text-align: left;"><br/></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902); background-position: initial initial; background-repeat: initial initial;-en-codeblock:true;"><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">import tensorflow as tf</span></div><div><br style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"/></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">#第一个是参数名称，第二个参数是默认值，第三个是参数描述</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">tf.app.flags.DEFINE_string('str_name', 'def_v_1',&quot;descrip1&quot;)</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">tf.app.flags.DEFINE_integer('int_name', 10,&quot;descript2&quot;)</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">tf.app.flags.DEFINE_boolean('bool_name', False, &quot;descript3&quot;)</span></div><div><br style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"/></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">FLAGS = tf.app.flags.FLAGS</span></div><div><br style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"/></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">#必须带参数，否则：'TypeError: main() takes no arguments (1 given)'; main的参数名随意定义，无要求</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">def main(_):</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"><span>    </span>print(FLAGS.str_name)</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"><span>    </span>print(FLAGS.int_name)</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"><span>    </span>print(FLAGS.bool_name)</span></div><div><br style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"/></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">if __name__ == '__main__':</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"><span>    </span>tf.app.run() #执行main函数</span></div></div><div style="text-align: left;"><br/></div><div style="text-align: left;"><span style="-en-paragraph: true;">执行：</span></div><div style="text-align: left;"><br/></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902); background-position: initial initial; background-repeat: initial initial;-en-codeblock:true;"><div>[root@AliHPC-G41-211 test]# python tt.py</div><div>def_v_1</div><div>10False</div><div>[root@AliHPC-G41-211 test]# python tt.py --str_name test_str --int_name 99 --bool_name True</div><div>test_str</div><div>99</div><div>True</div></div><div style="text-align: left;"><br/></div><div style="text-align: left;"><br/></div><div style="text-align: left;"><br/></div><div style="text-align: left;"><br/></div><div style="text-align: left;"><br/></div><div style="text-align: left;"><br/></div><div style="text-align: left;"><br/></div></span>
</div></body></html> 